package com.fh.common.util;

import java.io.*;

/**
 *  序列化工具类
 * @author junqiu
 *
 */
public class SerializeUtil {

	/**
	 * 序列化对象为二进制数组
	 * 
	 * @param object
	 * @return
	 */
	public static byte[] serialize(Object object) {
		ObjectOutputStream objectOutputStream = null;
		ByteArrayOutputStream byteArrayOutputStream = null;
		try {
			byteArrayOutputStream = new ByteArrayOutputStream();
			objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
			objectOutputStream.writeObject(object);
			byte[] getByte = byteArrayOutputStream.toByteArray();
			return getByte;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 反序列化二进制数组为对象
	 * 
	 * @param binaryByte
	 * @return
	 */
	public static Object unserizlize(byte[] binaryByte) {
		ObjectInputStream objectInputStream = null;
		ByteArrayInputStream byteArrayInputStream = null;
		byteArrayInputStream = new ByteArrayInputStream(binaryByte);
		try {
			objectInputStream = new ObjectInputStream(byteArrayInputStream);
			Object obj = objectInputStream.readObject();
			return obj;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
